﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System;
using System.Linq.Expressions;

namespace Tasks.Common
{
    public static class CheckBoxListHelper
    {

        #region "checkboxlist"

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items)
        {
            return CheckBoxList(helper, name, items, null, null);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, IDictionary<string, object> checkboxHtmlAttributes)
        {
            return CheckBoxList(helper, name, items, null, checkboxHtmlAttributes);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, IEnumerable<string> selectedValues)
        {
            return CheckBoxList(helper, name, items, selectedValues, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="items"></param>
        /// <param name="selectedValues"></param>
        /// <param name="checkboxHtmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, IEnumerable<string> selectedValues, IDictionary<string, object> checkboxHtmlAttributes)
        {
            var selectListItems = from i in items
                                  select new SelectListItem
                                             {
                                                 Text = i.Key,
                                                 Value = i.Value,
                                                 Selected = (selectedValues != null && selectedValues.Contains(i.Value))
                                             };

            return CheckBoxList(helper, name, selectListItems, checkboxHtmlAttributes);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items)
        {
            return CheckBoxList(helper, name, items, null);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items, IDictionary<string, object> checkboxHtmlAttributes)
        {
            var output = new StringBuilder();

            foreach (var item in items)
            {

                var checkboxList = new TagBuilder("input");
                checkboxList.MergeAttribute("type", "checkbox");
                checkboxList.MergeAttribute("name", name);
                checkboxList.MergeAttribute("value", item.Value);

                // Check to see if it’s checked
                if (item.Selected)
                    checkboxList.MergeAttribute("checked", "checked");

                // Add any attributes
                if (checkboxHtmlAttributes != null)
                    checkboxList.MergeAttributes(checkboxHtmlAttributes);

                checkboxList.SetInnerText(item.Text);
                output.Append(checkboxList.ToString(TagRenderMode.SelfClosing));
                output.Append(item.Text + "&nbsp;");
            }

            return MvcHtmlString.Create(output.ToString());
        }

        #endregion

        #region "RadionButtonList"

        public static MvcHtmlString RadionButtonList(this HtmlHelper helper, string name, IDictionary<string, string> items)
        {
            return RadionButtonList(helper, name, items, null, null);
        }

        public static MvcHtmlString RadionButtonList(this HtmlHelper helper, string name, IDictionary<string, string> items, IDictionary<string, object> checkboxHtmlAttributes)
        {
            return RadionButtonList(helper, name, items, null, checkboxHtmlAttributes);
        }

        public static MvcHtmlString RadionButtonList(this HtmlHelper helper, string name, IDictionary<string, string> items, IEnumerable<string> selectedValues)
        {
            return RadionButtonList(helper, name, items, selectedValues, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <param name="items"></param>
        /// <param name="selectedValues"></param>
        /// <param name="checkboxHtmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString RadionButtonList(this HtmlHelper helper, string name, IDictionary<string, string> items, IEnumerable<string> selectedValues, IDictionary<string, object> checkboxHtmlAttributes)
        {
            var selectListItems = from i in items
                                  select new SelectListItem
                                  {
                                      Text = i.Key,
                                      Value = i.Value,
                                      Selected = (selectedValues != null && selectedValues.Contains(i.Value))
                                  };

            return RadionButtonList(helper, name, selectListItems, checkboxHtmlAttributes);
        }

        public static MvcHtmlString RadionButtonList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items)
        {
            return RadionButtonList(helper, name, items, null);
        }

        public static MvcHtmlString RadionButtonList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items, IDictionary<string, object> checkboxHtmlAttributes)
        {
            var output = new StringBuilder();

            foreach (var item in items)
            {

                var RadionButtonList = new TagBuilder("input");
                RadionButtonList.MergeAttribute("type", "radio");
                RadionButtonList.MergeAttribute("name", name);
                RadionButtonList.MergeAttribute("value", item.Value);

                // Check to see if it’s checked
                if (item.Selected)
                    RadionButtonList.MergeAttribute("checked", "checked");

                // Add any attributes
                if (checkboxHtmlAttributes != null)
                    RadionButtonList.MergeAttributes(checkboxHtmlAttributes);

                RadionButtonList.SetInnerText(item.Text);
                output.Append(RadionButtonList.ToString(TagRenderMode.SelfClosing));
                output.Append(item.Text + "&nbsp;");
            }

            return MvcHtmlString.Create(output.ToString());
        }

        #endregion


    }
}